package com.sjq.common.web;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.entity.Menu;
import com.sjq.common.entity.Result;
import com.sjq.common.service.ApplicationSystemService;
import com.sjq.common.service.MenuManagerService;
import com.sjq.common.service.MenuTreeService;

/**
 * 菜单管理
 * @author xc
 * @version 2018.02.05
 * @since jdk1.6
 *
 */
@RestController
@RequestMapping(value = "/menu/web/api/menuManagerController")
public class MenuManagerController {
	//应用系统信息 逻辑层
	@Resource
	private ApplicationSystemService applicationSystemService;
	
	//菜单管理 逻辑层
	@Resource
	private MenuManagerService menuManagerService;
	
	//菜单树
	@Resource
	private MenuTreeService menuTreeService;
	/**
	 * 菜单管理显示页面,主要显示应用系统信息
	 * @return
	 */
	@RequestMapping("/managerView")
	public ModelAndView managerView(){
		ModelAndView modelAndView =new ModelAndView("/view/menu/managerView");
		return modelAndView;
	}
	/**
	 * 获取应用系统信息数据
	 * @return
	 */
	@RequestMapping("/getApplicationSystemData")
	@ResponseBody
	public String getApplicationSystemData(@RequestParam(value="pageNo") int pageNo){
		String json=applicationSystemService.findByPage(pageNo);
		
		return json;
	}
	
	/**
	 * 菜单查询页面,主要查询菜单,可以跳转到增删改页面
	 * @return
	 */
	@RequestMapping("/menuView")
	public ModelAndView menuView(){
		ModelAndView modelAndView =new ModelAndView("/view/menu/menuView");
		return modelAndView;
	}
	/**
	 * 获取所有菜单信息数据
	 * @return
	 */
	@RequestMapping("/getMenuData")
	@ResponseBody
	public String getMenuData(){
		List<Menu> menuList=menuManagerService.findMenuList();
		String json=JSONArray.fromObject(menuList).toString();
		return json;
	}
	/**
	 * 根据应用系统id,获取菜单信息
	 * @return
	 */
	@RequestMapping("/getMenuBySystemId")
	@ResponseBody
	public String getMenuBySystemId(@RequestParam(value="systemId") String systemId){
		List<Menu> menuList=menuManagerService.findBySystemId(systemId);
		String json=JSONArray.fromObject(menuList).toString();
		return json;
	}
	/**
	 * 根据parentMenuId 查找子菜单
	 * @param systemId
	 * @param parentMenuId
	 * @return
	 */
	@RequestMapping("/getMenuByParentMenuId")
	@ResponseBody
	public String getMenuByParentMenuId(@RequestParam(value="systemId") String systemId,@RequestParam(value="parentMenuId")String parentMenuId){
		String str=menuManagerService.findByParentMenuId(systemId,parentMenuId);
		return str;
	}
	
	/**
	 * 菜单新增页面
	 * @return
	 */
	@RequestMapping("/menuAddView")
	public ModelAndView menuAddView(){
		ModelAndView modelAndView=new ModelAndView("/view/menu/menuAddView");
		
		return modelAndView;
	}
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@RequestMapping("/saveMenu")
	@ResponseBody
	public String saveMenu(HttpServletRequest request,Menu menu){
		//获取session
		HttpSession session=request.getSession();
		LoginAccount loginAcount=(LoginAccount)session.getAttribute("account");
		//设置登记单位
		menu.setDjdw_gajgjgdm(loginAcount.getUserDwbm());
		//设置登记人用户名
		menu.setDjr_yhm(loginAcount.getUserId());
		return menuManagerService.saveMenu(menu);
	}
	
	/**
	 * 菜单编辑页面
	 * @return
	 */
	@RequestMapping("/menuEditView")
	public ModelAndView menuEditView(@RequestParam("xxzjbh") String xxzjbh){
		ModelAndView modelAndView=new ModelAndView("/view/menu/menuEditView");
		modelAndView.addObject("menu",menuManagerService.findByXxzjbh(xxzjbh));
		modelAndView.addObject("m","33");
		return modelAndView;
		
	}
	/**
	 * 根据信息主键编号 ,修改菜单
	 * @param xxzjbh
	 * @return
	 */
	@RequestMapping("/updateMenu")
	@ResponseBody
	public String updateMenu(Menu menu){
		
		return menuManagerService.updateMenu(menu);
	}
	/**
	 * 根据信息主键编号,删除菜单
	 * @param xxzjbh
	 * @return
	 */
	@RequestMapping("/deleteMenu")
	@ResponseBody
	public String deleteMenu(@RequestParam("xxzjbh") String xxzjbh){
		return menuManagerService.deleteMenu(xxzjbh);
	}
	
	/**
	 * 更新拖拽节点
	 * @param xxzjbh 拖拽 根节点信息主键编号
	 * @param menuId 拖拽 根节点菜单id
	 * @param targetParentMenuId 目标节点菜单id 
	 * @return
	 */
	@RequestMapping("/updateDrop")
	@ResponseBody
	public Result updateDrop(String xxzjbh,String parentMenuId){
		Result result=new Result();
		try {
			result=menuManagerService.updateDrop(xxzjbh, parentMenuId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			result.setStatus(1);
			result.setMsg("拖拽失败!");
		}
		
		return result;
	}
	
	/**
	 * 更新排序号
	 * @param jsonArrayStr
	 * @return
	 */
	@RequestMapping("/updateOrderno")
	@ResponseBody
	public Result updateOrderno(String jsonArrayStr){
		JSONArray jsonArray=JSONArray.fromObject(jsonArrayStr);
		Result result=new Result();
		try {
			result=menuManagerService.updateDropOrderNo(jsonArray);
		} catch (RuntimeException e) {
			e.printStackTrace();
			result.setStatus(1);
			result.setMsg("更新失败!");
		}
		return result;
	}
	
	/**更新updateAuthorityMenu**/
	@RequestMapping("/updateAuthorityMenu")
	@ResponseBody
	public String updateAuthorityMenu(String nodeArray,String authorityId){
		menuTreeService.updatePrivilege(nodeArray, authorityId);
		return "";
	}
	
	/**查看*/
	@RequestMapping("/findZtreeMenusByAuthority")
	@ResponseBody
	public List<Map<String, String>> findZtreeMenusByAuthority(String authorityId){
		return menuTreeService.queryMenusByAuthority(authorityId);
	}
}
